<?php

namespace app\store\controller;

use app\store\model\Order as OrderModel;
use app\api\model\OrderAddress;
use app\store\model\User;
use think\Request;

/**
 * 订单管理
 * Class Order
 * @package app\store\controller
 */
class Order extends Controller
{
    /**
     * 待发货订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function delivery_list()
    {
        return $this->getList('待发货订单列表', [
            'pay_status' => 20,
            'delivery_status' => 10
        ]);
    }

    /**
     * 待收货订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function receipt_list()
    {
        return $this->getList('待收货订单列表', [
            'pay_status' => 20,
            'delivery_status' => 20,
            'receipt_status' => 10
        ]);
    }

    /**
     * 待付款订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function pay_list()
    {
        return $this->getList('待付款订单列表', ['pay_status' => 10, 'order_status' => 10]);
    }

    /**
     * 待服务订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function service_list()
    {
        return $this->getList('待服务订单列表', ['pay_status' => 20, 'b_id' => ['>', -1], 'service_status' => 10, 'delivery_status' => 30, 'order_status' => 10]);
    }

    /**
     * 已完成订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function complete_list()
    {
        return $this->getList('已完成订单列表', ['order_status' => 30]);
    }

    /**
     * 已取消订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function cancel_list()
    {
        return $this->getList('已取消订单列表', ['order_status' => 20]);
    }

    /**
     * 全部订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function all_list()
    {
        return $this->getList('全部订单列表');
    }

    /**
     * 订单列表
     * @param $title
     * @param $filter
     * @return mixed
     * @throws \think\exception\DbException
     */
    private function getList($title, $filter = [])
    {
        $model = new OrderModel;
        $list = $model->getList($filter);
        return $this->fetch('index', compact('title','list'));
    }

    /**
     * 订单详情
     * @param $order_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail($order_id)
    {
        $detail = OrderModel::detail($order_id);
        return $this->fetch('detail', compact('detail'));
    }

    /**
     * 确认发货
     * @param $order_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delivery($order_id)
    {
        $model = OrderModel::detail($order_id);
        if ($model->delivery($this->postData('order'))) {
            return $this->renderSuccess('发货成功');
        }
        $error = $model->getError() ?: '发货失败';
        return $this->renderError($error);
    }

    /**
     * 指定配送员
     * @param $goods_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function edits()
    {

        #--接收前端参数：
        $data=Request::instance()->param();#halt($data);

        #--拦截：
        if (!(new OrderModel())->update($data)) {
            return $this->renderError('修改失败');
        }

        #--查询发送模板消息所需参数：
        $user = new \app\api\model\User();

        #--测试：
        $formId = $user->getFormId($data['p_id']);

        $userInfo = $user->get(['user_id' => $data['p_id']]);
        $address = OrderAddress::get(['order_id' => $data['order_id']]);
        $param = [
            'touser' => $userInfo['open_id'],
            'template_id' => 'aPyXk5uJbbU9gd-6kKQvINaKxWCt3OIG-JzDEkASKMU',
            'url' => "https://" . $_SERVER['SERVER_NAME'],
            'form_id' => $formId[0],
            'topcolor' => "#FF0000",
            'data' => array(
                #'first' => array('value' => "你有新的订单", 'color' => "#fc0101"),
                'keyword1' => array('value' => $data['order_no'], 'color' => "#173177"),    #--订单号
                'keyword2' => array('value' => date('Y-m-d H:i:s', time()), 'color' => "#173177"),    #--配送时间
                'keyword3' => array('value' => $address['name'], 'color' => "#173177"),    #--用户
                'keyword4' => array('value' => $address['phone'], 'color' => "#173177"),    #--联系电话
                'keyword5' => array('value' => $address['region']['city']."  ".$address['region']['region']."  ".$address['detail'], 'color' => "#173177"),    #--收货地址
                #'remark' => array('value' => "新订单已产生，请及时接单", 'color' => "#173177"),
            )
        ];
        send_template_message($param);

        #--修改配送员工作状态：
        (new User)->save(['isworking' => 'Y'], ['open_id' => $userInfo['open_id']]);

        return $this->renderSuccess('修改成功');
    }

}
